package ui;

/**
 * EDCToolBarPanel
 * Component in panel that allows user to compute different operations on DTDs
 */
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import utils.EDCResource;

import java.net.URL;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EDCToolBarPanel extends JPanel
implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected String newline = "\n";
	static final private String ADD_BUTTON = "Add DTD";
	static final private String PREF = "Preferences";
	static final private String INFOS = "DTD Informations";

	public EDCToolBarPanel() {
		super(new BorderLayout());

		//Create the toolbar.
		JToolBar toolBar = new JToolBar("EDC Toolbar");
		addButtons(toolBar);

		//Lay out the main panel.
		setPreferredSize(new Dimension(750, 30));
		add(toolBar, BorderLayout.PAGE_START);
	}

	protected void addButtons(JToolBar toolBar) {
		JButton button = null;

		//first button
		button = makeNavigationButton("img2", PREF,
				"System configuration", PREF);
		toolBar.add(button);

		//second button

		button = makeNavigationButton("img1", ADD_BUTTON,
				"Add DTD from URL or local file", ADD_BUTTON);
		toolBar.add(button);

		//third button
		button = makeNavigationButton("img3", INFOS,
				"Refresh", INFOS);
		toolBar.add(button);
	}

	protected JButton makeNavigationButton(String imageName,
			String actionCommand,
			String toolTipText,
			String altText) {
		//Look for the image.
		String imgLocation = "images/"
			+ imageName
			+ ".gif";
		URL imageURL = EDCToolBarPanel.class.getResource(imgLocation);

		//Create and initialize the button.
		JButton button = new JButton();
		button.setActionCommand(actionCommand);
		button.setToolTipText(toolTipText);
		button.addActionListener(this);

		if (imageURL != null) {                      //image found
			button.setIcon(new ImageIcon(imageURL, altText));
		} else {                                     //no image found
			button.setText(altText);
			System.err.println("Resource not found: "
					+ imgLocation);
		}

		return button;
	}

	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();

		// Handle each button.
		if (PREF.equals(cmd)) { // first button clicked
			EDCResource.TEXT_VIEW_REQUIRED = false;
			EDCResource.DTD_VIEW_REQUIRED = false;
			EDCResource.ANALYZE_FILE_REQUIRED = false;
			EDCResource.ANALYZE_FOLDER_REQUIRED = false;
			EDCResource.ADD_DTD_REQUIRED = false;
			EDCResource.MANAGER_VIEW_REQUIRED = true;
			EDCResource.DTD_INFOS_REQUIRED = false;
			EDC.getCurrent().repaint();
			System.out.println(cmd);
		}
		else if (ADD_BUTTON.equals(cmd)) { //second button clicked
			EDCResource.TEXT_VIEW_REQUIRED = false;
			EDCResource.DTD_VIEW_REQUIRED = false;
			EDCResource.ANALYZE_FILE_REQUIRED = false;
			EDCResource.ANALYZE_FOLDER_REQUIRED = false;
			EDCResource.ADD_DTD_REQUIRED = true;
			EDCResource.MANAGER_VIEW_REQUIRED = false;
			EDCResource.DTD_INFOS_REQUIRED = false;
			EDC.getCurrent().repaint();
			System.out.println(ADD_BUTTON);
		}
		else if (INFOS.equals(cmd)) { // third button clicked
			EDCResource.TEXT_VIEW_REQUIRED = false;
			EDCResource.DTD_VIEW_REQUIRED = false;
			EDCResource.ANALYZE_FILE_REQUIRED = false;
			EDCResource.ANALYZE_FOLDER_REQUIRED = false;
			EDCResource.ADD_DTD_REQUIRED = false;
			EDCResource.MANAGER_VIEW_REQUIRED = false;
			EDCResource.DTD_INFOS_REQUIRED = true;
			EDC.getCurrent().repaint();
			System.out.println(INFOS);
		}

//		displayResult("If this were a real app, it would have "
//		+ description);
	}

}

